﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace BenefitHelper.Sys
{
    public class OperatorInfo
    {
        public int Id { get; set; }
        /// <summary>
        /// 交易员编号
        /// </summary>
        public int OperatorId { get; set; }
        /// <summary>
        /// 交易员
        /// </summary>
        public virtual Operator Operator { get; set; }
        public int AllTradeDays { get; set; }
        /// <summary>
        /// 上次登陆时间
        /// </summary>
        public DateTime LastLoginTime { get; set; }
        /// <summary>
        /// 首次交易时间
        /// </summary>
        public int FirstTradeHistoryId { get; set; }
        /// <summary>
        /// 末次交易时间
        /// </summary>
        public int LastTradeHistoryId { get; set; }
        /// <summary>
        /// 增加一个操作员信息，每个操作员只有一条操作员信息
        /// </summary>
        public void AddOperatorInfo(DB.DBManager db)
        {
            OperatorInfo info = new OperatorInfo();
            info.AllTradeDays = 0;
            info.FirstTradeHistoryId = 1;
            info.LastLoginTime = DateTime.Now;
            info.LastTradeHistoryId = 1;
            info.OperatorId = this.OperatorId;
            db.OperatorInfo.Add(info);
            db.SaveChanges();
        }
        /// <summary>
        /// 删除指定日期的结算记录时把交易天数减一
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void UnsetDayWorking(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.Operators.ToList();
            foreach (Sys.Operator op in query)
            {
                View.OperatorDayChange change = new View.OperatorDayChange().GetOperatorDayChange(op.Id, tradeHistoryId, db);
                if (change.BillCount > 0)
                {
                    var _query = db.OperatorInfo.Where(a => a.OperatorId == op.Id).ToList();
                    _query.First().AllTradeDays = _query.First().AllTradeDays - 1;
                }
            }
            db.SaveChanges();
        }
        public void OperatorWorking(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.Operators.ToList();
            foreach (Sys.Operator op in query)
            {
                View.OperatorDayChange change = new View.OperatorDayChange().GetOperatorDayChange(op.Id, tradeHistoryId, db);
                if (change.BillCount > 0)
                {
                    var _query = db.OperatorInfo.Where(a => a.OperatorId == op.Id).ToList();
                    if (_query.Count() > 0)//多次交易
                    {
                        OperatorInfo info = _query.First();
                        _query.First().AllTradeDays = info.AllTradeDays + 1;
                        _query.First().LastTradeHistoryId = tradeHistoryId;
                        db.SaveChanges();
                    }
                    else//首次交易
                    {
                        OperatorInfo info = new OperatorInfo();
                        info.AllTradeDays = 1;
                        info.FirstTradeHistoryId = tradeHistoryId;
                        info.LastTradeHistoryId = tradeHistoryId;
                        info.LastLoginTime = DateTime.Now;
                        info.OperatorId = op.Id;
                        db.OperatorInfo.Add(info);
                        db.SaveChanges();
                    }
                }
            }
        }

        public OperatorInfo GetOperatorInfo(int operatorid, DB.DBManager db)
        {
            OperatorInfo info = null;
            var query = db.OperatorInfo.Where(a => a.OperatorId == operatorid).ToList();
            if (query.Count() > 0)
            {
                info = query.First();
            }
            return info;
        }

        internal List<OperatorInfo> GetList(DB.DBManager db)
        {
            return db.OperatorInfo.ToList();
        }
    }
}